package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.product.dto.admin.product.ProductDetailDTO;
import com.chushouya.product.dto.admin.product.ProductListDTO;
import com.chushouya.product.dto.admin.product.query.ProductQuery;
import com.chushouya.product.service.admin.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = ServiceType.PLATFORM + "商品管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @ApiOperation("获取商品列表")
    @GetMapping("/list")
    public Response<PageResult<ProductListDTO>> list(ProductQuery query) {
        PageResult<ProductListDTO> pageResult = productService.selectProductPageResult(query);
        return Response.success(pageResult);
    }

    @ApiOperation("获取详细信息")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", required = true),
    })
    public Response<ProductDetailDTO> getDetail(Long productId) {
        return Response.success(productService.getProductDetail(productId));
    }

    @ApiOperation("商品上架")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/setOnline")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", required = true),
    })
    public Response<Void> setOnline(Long productId) {
        productService.setOnline(productId);
        return Response.success();
    }

    @ApiOperation("商品下架")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/setOffline")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", required = true),
    })
    public Response<Void> setOffline(Long productId) {
        productService.setOffline(productId);
        return Response.success();
    }

    @ApiOperation("删除商品")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", required = true),
    })
    public Response<Void> remove(Long productId) {
        productService.removeProduct(productId);
        return Response.success();
    }

    @ApiOperation("编辑商品排序")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/editProductSort")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true),
    })
    public Response<Void> editProductSort(Long productId, Integer sort) {
        productService.editProductSort(productId, sort);
        return Response.success();
    }

}
